local Biaoqing = fk.CreateSkill {
    name = "Biaoqing",
    anim_type = "special",
}

Fk:loadTranslationTable{
  ["$biaoqing"] = "表情",
  ["Biaoqing"] = "表情",
  ["@@biaoqing-inarea"] = "表情",
  [":Biaoqing"] = "出牌阶段，你可以将一张牌盖在一名其他角色的武将牌上，称为“表情”。若武将牌上有“表情”的角色使用与“表情”牌类型相同的牌时随机执行以下一种效果：①此牌再结算一次②无效③随机重铸自己的一张手牌④将自己的一张牌移出游戏。你可以将“表情”牌当手牌般使用。",
  ["#Biaoqing-target"] = "表情：选择一名角色，将一张牌背面朝上置于其武将牌上称为“表情”<br>你可随时使用“表情”牌，有“表情”牌的角色使用牌时随机触发效果。",
  ["#Biaoqing_effect1"] = "%from 的技能【表情】被触发，%arg 的效果重复结算一次",
  ["#Biaoqing_effect2"] = "%from 的技能【表情】被触发，%arg 无效",
  ["#Biaoqing_effect3"] = "%from 的技能【表情】被触发，其重铸了一张手牌",
  ["#Biaoqing_effect4"] = "%from 的技能【表情】被触发，其将一张牌移出了游戏",
}

-- 出牌阶段主动技能
Biaoqing:addEffect("active", {
  prompt = "#Biaoqing-target",
  card_num = 1,
  target_num = 1,
  can_use = function(self, player)
    return not player:isNude()
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0
  end,
  target_filter = function(self, player, to_select, selected, cards)
    return to_select ~= player and #selected == 0
  end,
  on_use = function(self, room, effect)
    local target = effect.tos[1]
    local card = Fk:getCardById(effect.cards[1])
    room:setCardMark(card, "@@biaoqing-inarea", 1)
    target:addToPile("$biaoqing", card, false, Biaoqing.name)
  end,
})

Biaoqing:addEffect("filter", {
  handly_cards = function (self, player)
      local ids = {}
      if player:hasSkill(Biaoqing.name) then
        for _, p in ipairs(Fk:currentRoom().alive_players) do
          table.insertTableIfNeed(ids, p:getPile("$biaoqing"))
        end
        return ids
      end
  end,
})

-- 监听卡牌使用，触发随机效果
Biaoqing:addEffect(fk.CardUsing, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(Biaoqing.name) then
      local card_type = data.card:getTypeString()
      local has_same_type = false
      for _, id in ipairs(target:getPile("$biaoqing")) do
        local equip_card = Fk:getCardById(id)
        if equip_card:getTypeString() == card_type then
          has_same_type = true
          break
        end
      end

      return has_same_type
    end
  end,
  
  on_use = function(self, event, target, player, data)
    local room = player.room
    
    local random_effect = math.random(1, 4)
    
    if random_effect == 1 then
      -- ①重复结算一次
      room:sendLog{
        type = "#Biaoqing_effect1",
        from = player.id,
        arg = data.card:toLogString(),
      }
      data.additionalEffect = (data.additionalEffect or 0) + 1
      
    elseif random_effect == 2 then
      -- ②无效
      room:sendLog{
        type = "#Biaoqing_effect2",
        from = player.id,
        arg = data.card:toLogString(),
      }
      
      -- 标记卡牌无效
      data.toCard = nil
      data:removeAllTargets()
      
    elseif random_effect == 3 then
      -- ③随机重铸一张手牌
      room:sendLog{
        type = "#Biaoqing_effect3",
        from = player.id,
      }
      if not target:isKongcheng() then
        local hand_cards = target:getCardIds(Player.Hand)
        local random_card = hand_cards[math.random(1, #hand_cards)]
        room:recastCard({random_card}, target, Biaoqing.name)
      end
      
    elseif random_effect == 4 then
      -- ④将一张牌移出游戏
      room:sendLog{
        type = "#Biaoqing_effect4",
        from = player.id,
      }
      
      -- 从玩家区域中随机选择一张牌移出游戏
      local all_cards = {}
      for _, id in ipairs(target:getCardIds(Player.Hand)) do
        table.insert(all_cards, id)
      end
      for _, id in ipairs(target:getCardIds(Player.Equip)) do
        table.insert(all_cards, id)
      end
      
      if #all_cards > 0 then
        local random_card = all_cards[math.random(1, #all_cards)]
        
        -- 移出游戏
        room:moveCardTo(random_card, Card.Void, nil, fk.ReasonJustMove, Biaoqing.name, nil, true, target.id)
      end
    end
  end,
})

return Biaoqing 